package com.jiang.jetpackdemo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.model.ImgInfo
import com.jiang.jetpackdemo.utils.loadPicFromNet

/**
 *
 * @ProjectName:    JetpackDemo
 * @ClassName:      ImgAdapter
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/6/7 22:39
 */
class ImgAdapter(val context: Context, val list: MutableList<ImgInfo>) :
    RecyclerView.Adapter<ImgAdapter.ViewHolder>() {
    class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        val img: ImageView = view.findViewById(R.id.iv_pic)
        val btn: Button = view.findViewById(R.id.btn)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_pic, parent, false))


    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.apply {
            val item = list[position]
            img.loadPicFromNet(item.imgurl)
            btn.setOnLongClickListener {
                onLongClickListener?.invoke(position,view)
                false
            }
        }

    }

    private var onLongClickListener: ((Int,View) -> Unit)? = null
    fun setLongClickListener(onLongClick: (Int,View) -> Unit) {
        onLongClickListener = onLongClick
    }
}